package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther:
 * @Description:
 * @Date: 2019/11/19 15:20
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService iDeptService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/add")
//    @RequestMapping(value = "/add")
    public boolean add(@RequestBody Dept dept) {
        System.out.println("8001-add");
        return iDeptService.add(dept);
    }

    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        System.out.println("8001-get");
        return iDeptService.get(id);
    }

    @GetMapping("/list")
    public List<Dept> list(){
        System.out.println("8001-list");
        return iDeptService.list();
    }

    @RequestMapping("/discovery")
    public Object discovery(){
        System.out.println("8001-discovery");
        List<String> services = discoveryClient.getServices();
        System.out.println("***" + services);
        List<ServiceInstance> instances = discoveryClient.getInstances("MICROSERVICECLOUD-DEPT");
        for (ServiceInstance element: instances) {
            System.out.println(element.getServiceId() + "\t"
                + element.getHost() + "\t"
                + element.getPort() + "\t"
                + element.getUri());
        }
        return this.discoveryClient;
    }
}
